1 00:00:00,720 --> 00:00:01,680 Welcome back. 2 00:00:01,680 --> 00:00:06,030 In this lecture we're going to be checking to see if our bullets hit any limbs. 3 00:00:06,030 --> 00:00:10,500 And if they do, we'll display a hit marker around the crosshair of our mouse. 4 00:00:10,500 --> 00:00:16,170 So as an example here, if I go and I shoot this character right here and I click, as you can see, 5 00:00:16,170 --> 00:00:18,750 a little hit marker appeared around my crosshair. 6 00:00:18,750 --> 00:00:23,490 And if I get a headshot, as you can see it displays a red hit marker. 7 00:00:23,490 --> 00:00:28,410 And if I continue to shoot these guys, as you can see, I'm getting hit markers on my mouse. 8 00:00:30,070 --> 00:00:32,410 Back inside of the local script for our guns. 9 00:00:32,410 --> 00:00:38,020 What we want to do is we want to go ahead and create a dedicated function for displaying a hit marker 10 00:00:38,020 --> 00:00:44,860 on the screen, and we want to pass to this function the result from our raycast. 11 00:00:44,860 --> 00:00:48,610 And then this function can go ahead and check to see if we hit anything. 12 00:00:48,610 --> 00:00:53,650 And if the thing we hit belonged to a character model and it's not a player's character model. 13 00:00:53,650 --> 00:00:58,060 And then from that point it can get the instance that was hit, check to see if it was a head, and 14 00:00:58,060 --> 00:01:00,160 then we can display a red hit marker. 15 00:01:00,160 --> 00:01:03,460 Otherwise we can display a normal white hit marker. 16 00:01:04,040 --> 00:01:10,010 So for example, we would call this function from our shoot function if we were shooting a standard 17 00:01:10,010 --> 00:01:10,310 gun. 18 00:01:10,310 --> 00:01:15,770 After we perform the raycast, we could go ahead and call our display, hit marker function and pass 19 00:01:15,770 --> 00:01:16,730 our result. 20 00:01:16,730 --> 00:01:21,800 And then from this point we would go in here and check to see if we actually had a raycast result. 21 00:01:22,310 --> 00:01:26,570 If we did not have a raycast result, then we can simply return. 22 00:01:26,990 --> 00:01:32,480 Otherwise we can go ahead and see if this was a character model by getting the result dot instance dot 23 00:01:32,480 --> 00:01:33,290 parent. 24 00:01:34,640 --> 00:01:44,990 And then if inside of the parent of this instance we do not find first child which is a humanoid, then 25 00:01:44,990 --> 00:01:46,400 we can also return. 26 00:01:46,430 --> 00:01:51,680 Otherwise, we also want to verify that this character does not belong to a player in our game. 27 00:01:51,680 --> 00:01:59,390 So if players get player from character and we pass the character, if we actually get a result from 28 00:01:59,390 --> 00:02:01,790 this function, then we're just going to return. 29 00:02:02,150 --> 00:02:08,360 Otherwise, we can go ahead and play the hit marker sound and display the hit marker on our screen. 30 00:02:08,360 --> 00:02:10,670 So we would create a new sound here. 31 00:02:11,030 --> 00:02:12,140 So we'll create sound. 32 00:02:12,140 --> 00:02:17,870 And that sound is going to be parented to we could do the workspace since we want to play it globally 33 00:02:17,870 --> 00:02:20,330 around the entire headphone of our player. 34 00:02:20,330 --> 00:02:23,360 We don't want it to be a point in the map. 35 00:02:23,360 --> 00:02:26,840 We would just want it to play normally as a sound for our player. 36 00:02:26,840 --> 00:02:31,460 So we'll parent it to the workspace and then the sound is going to be the properties dot hit marker 37 00:02:31,460 --> 00:02:32,900 sound properties. 38 00:02:33,020 --> 00:02:35,090 Then we can go ahead and play the sound. 39 00:02:35,270 --> 00:02:38,660 And then when the sound ends we can go ahead and delete it. 40 00:02:38,660 --> 00:02:40,280 So sound destroy. 41 00:02:41,470 --> 00:02:45,970 And then from this point, we could go ahead and check to see if the instance inside of the result we 42 00:02:46,000 --> 00:02:47,950 hit is a head. 43 00:02:47,950 --> 00:02:55,270 So if result dot instance dot name is equal to head, then we want to go ahead and update our hit marker 44 00:02:55,270 --> 00:02:56,710 to be a red color. 45 00:02:56,710 --> 00:03:02,500 So if you remember inside of each one of our guys we have an image label here called hit marker. 46 00:03:02,500 --> 00:03:05,110 So that means we can go ahead and modify the image color. 47 00:03:05,110 --> 00:03:07,180 So we'll refer to our guy. 48 00:03:07,740 --> 00:03:13,410 And we'll go ahead and get the hip marker, and we can update the image color three to a new color three 49 00:03:13,410 --> 00:03:14,550 from RGB. 50 00:03:14,550 --> 00:03:16,440 And we want it to be a red color. 51 00:03:16,440 --> 00:03:18,360 So we could just slide this up here. 52 00:03:18,480 --> 00:03:20,070 And then there's our red color. 53 00:03:20,070 --> 00:03:21,330 We'll hit okay. 54 00:03:22,120 --> 00:03:26,410 Otherwise, if we did not hit a head, we can go ahead and set the geo hit marker. 55 00:03:26,530 --> 00:03:28,690 Image color three back to be white. 56 00:03:28,690 --> 00:03:32,890 So color three from RGB and that's just going to be pure white. 57 00:03:32,890 --> 00:03:33,820 There we go. 58 00:03:33,820 --> 00:03:38,590 And then when we can go ahead and do is we can go ahead and make sure the hit marker is visible. 59 00:03:38,590 --> 00:03:40,540 So we'll set visible equal to true. 60 00:03:40,540 --> 00:03:45,070 And then we'll delay a function for a very short period of time. 61 00:03:45,070 --> 00:03:49,660 We could do like 0.1 seconds or maybe a little bit less like 0.08 seconds. 62 00:03:49,660 --> 00:03:55,510 And all this function is going to do is set guy hit marker dot visible back to false. 63 00:03:55,960 --> 00:04:00,850 So now we're displaying the hit marker on the screen for a very short period of time. 64 00:04:00,850 --> 00:04:05,710 But the next problem we need to go ahead and solve is we want to make sure that the position of this 65 00:04:05,710 --> 00:04:11,950 hit marker on our screen is equal to the exact same position of our mouse on the screen. 66 00:04:12,490 --> 00:04:18,460 So what we can do for that is there is an event inside of our mouse object called move, and it says 67 00:04:18,460 --> 00:04:20,020 fired when the mouse is moved. 68 00:04:20,020 --> 00:04:21,880 So we can listen to this event. 69 00:04:21,880 --> 00:04:26,920 And every single time this event fires, we can update the position of our hit marker to be equal to 70 00:04:26,920 --> 00:04:28,960 the position of our mouse. 71 00:04:28,960 --> 00:04:33,730 So what I want to do is I only want to listen to this event when I have my gun equipped, and then I 72 00:04:33,730 --> 00:04:36,940 don't want to listen to the event when I have the gun unequipped. 73 00:04:36,940 --> 00:04:39,970 So we can go ahead and make a variable up here. 74 00:04:39,970 --> 00:04:44,980 I'm going to call this mouse connection, and this is going to store a connection to that event. 75 00:04:44,980 --> 00:04:49,240 And that way when we unequip our gun we can go ahead and disconnect that connection. 76 00:04:49,930 --> 00:04:52,900 Then we can go down to our equipped event. 77 00:04:53,940 --> 00:05:00,630 And inside of here we can go ahead and listen to the mouse move event, connect a function to this. 78 00:05:00,630 --> 00:05:04,860 And this is where we're going to update the position of that hit marker. 79 00:05:04,860 --> 00:05:08,850 And then we can store this connection inside of our mouse connection variable. 80 00:05:09,340 --> 00:05:16,600 And inside of here, all we're going to do is set guy hitmarker dot position equal to a new Utum two. 81 00:05:16,600 --> 00:05:19,000 And this is going to be from offset. 82 00:05:19,840 --> 00:05:22,030 Any position is going to be very simple. 83 00:05:22,030 --> 00:05:27,100 Inside of our mouse object, we can get the x and the y position on the screen. 84 00:05:27,100 --> 00:05:32,680 So we'll pass mouse dot x and then mouse dot y just like that. 85 00:05:33,220 --> 00:05:35,740 And then when we go and unequip our gun. 86 00:05:38,200 --> 00:05:42,430 We can go ahead and refer to mouse connection and disconnect it. 87 00:05:43,700 --> 00:05:44,720 Okay, cool. 88 00:05:44,720 --> 00:05:51,860 Now, the next important thing we need to do is we need to call our display hit marker function. 89 00:05:51,860 --> 00:05:54,590 Also from when we're shooting a shotgun. 90 00:05:54,590 --> 00:05:59,570 However, this one's going to be a little bit more involved because shotguns can have multiple pellets 91 00:05:59,570 --> 00:06:00,740 coming out of them. 92 00:06:00,830 --> 00:06:08,120 And what that means is some pellets might hit the head of a character model, while some might hit the 93 00:06:08,120 --> 00:06:08,660 body. 94 00:06:08,660 --> 00:06:15,380 If any of the pellets hit a head, then I want that one to take precedence and display the hit marker 95 00:06:15,380 --> 00:06:21,080 as red, while if none of the pellets hit the head but some hit the body, then we can make it a white 96 00:06:21,080 --> 00:06:21,890 hit marker. 97 00:06:22,560 --> 00:06:29,910 So what I'm going to do actually, is I want to loop through every single pellet result inside of our 98 00:06:29,910 --> 00:06:31,380 all pellets table. 99 00:06:32,320 --> 00:06:38,740 And we're going to go ahead and check to see if any of the names of the instances we hit is equal to 100 00:06:38,740 --> 00:06:40,000 the name of head. 101 00:06:40,000 --> 00:06:46,720 So if pellet result dot raycast result, we get the instance and the name of that instance if it's equal 102 00:06:46,720 --> 00:06:47,680 to head. 103 00:06:48,320 --> 00:06:51,950 Then that means we can go ahead and just display a hit marker. 104 00:06:52,700 --> 00:06:56,540 And we're going to pass the pelt result raycast result. 105 00:06:56,690 --> 00:06:58,850 And then we're just going to break out of this loop. 106 00:06:58,850 --> 00:07:01,940 So this will allow us to display a red hit marker. 107 00:07:02,950 --> 00:07:09,040 Now, the next thing we want to do is we want to listen to see if we got any body shots on our character 108 00:07:09,040 --> 00:07:09,760 model. 109 00:07:09,760 --> 00:07:18,040 So what we can do is we can check to see if the pelt result dot raycast result dot instance dot parent. 110 00:07:18,250 --> 00:07:22,930 We find first child which is a humanoid. 111 00:07:23,170 --> 00:07:29,470 If that's the case, then that means we can go ahead and display a hit marker or a white hit marker 112 00:07:29,470 --> 00:07:30,220 on our screen. 113 00:07:30,220 --> 00:07:31,480 Now here's a problem. 114 00:07:31,480 --> 00:07:34,750 We're looping through all of the results inside of the table. 115 00:07:34,750 --> 00:07:37,090 Let's say the first result was a body shot. 116 00:07:37,090 --> 00:07:42,190 Well, if we just display a white hit marker and then break out of this loop and display no other hit 117 00:07:42,190 --> 00:07:45,070 markers, well, we only went through the first result. 118 00:07:45,070 --> 00:07:50,230 There could have been a possibility where maybe the ninth result in our all pellets table was actually 119 00:07:50,230 --> 00:07:51,280 a head shot. 120 00:07:51,280 --> 00:07:56,320 So what we want to do is we actually want to create a table, and I'm going to call it body shots. 121 00:07:57,190 --> 00:08:03,520 And we're going to store in here all of the pellets that hit the body of a character model. 122 00:08:03,520 --> 00:08:09,100 So if we've confirmed that the name of an instance we hit was not equal to head. 123 00:08:10,000 --> 00:08:14,380 Then what we can go ahead and do is we can check to see if it's still belong to a character model, 124 00:08:14,380 --> 00:08:18,670 and if it did, we can insert that result into our body shots table. 125 00:08:18,670 --> 00:08:24,940 So body shots and the value we want to insert is going to be pellet result dot raycast result. 126 00:08:25,360 --> 00:08:31,360 Then when we're finally done looping what we can go ahead and do is we want to check to see if we got 127 00:08:31,360 --> 00:08:32,920 any body shots. 128 00:08:33,690 --> 00:08:39,630 So if the number of body shots is greater than zero, then that means we can go ahead and display a 129 00:08:39,660 --> 00:08:44,460 hit marker and we can just pass something like the first result in body shots. 130 00:08:44,490 --> 00:08:46,140 Now there's another issue here. 131 00:08:46,170 --> 00:08:51,960 Let's say we loop through this table here, and we find a couple of them that are body shots. 132 00:08:51,960 --> 00:08:53,790 And we insert those into the table. 133 00:08:53,790 --> 00:08:56,520 But then let's say like the fourth result was a head shot. 134 00:08:56,520 --> 00:08:59,970 And then we display that hit marker and then break out of this loop. 135 00:08:59,970 --> 00:09:04,140 Well, once we break out of this loop we've just displayed a red hit marker. 136 00:09:04,140 --> 00:09:10,380 And then since the body shots table still has stuff in it, we're going to display another hit marker. 137 00:09:10,380 --> 00:09:13,710 So we don't want to display two hit markers at the same time. 138 00:09:13,710 --> 00:09:19,680 So an easy way to fix that is we can check to see that once we actually got a head shot of something, 139 00:09:19,680 --> 00:09:25,980 then we can simply just set the body shots table to be an empty table break out of the loop. 140 00:09:25,980 --> 00:09:29,820 And that way we don't display another hit marker on the screen. 141 00:09:30,500 --> 00:09:35,030 And just like that, we should be complete with displaying hit markers on the screen. 142 00:09:35,030 --> 00:09:40,610 So let's go ahead and copy the assault rifle into the starter pack. 143 00:09:40,610 --> 00:09:44,720 And then I'm also going to copy a shotgun in here into the starter pack. 144 00:09:44,720 --> 00:09:49,070 And then I'll make sure to copy that updated local script inside of my shotgun. 145 00:09:49,160 --> 00:09:51,620 And then let's go ahead and playtest our game. 146 00:09:52,220 --> 00:09:54,140 So here I've got my assault rifle. 147 00:09:54,140 --> 00:10:00,620 If I shoot the ground, obviously I'm not going to get any hit marker, but let's say I shoot the hand 148 00:10:00,620 --> 00:10:02,960 of this character model if I shoot it. 149 00:10:03,530 --> 00:10:09,200 As you can see, it displayed the hit marker and I got the hit marker sound if I get a headshot, as 150 00:10:09,200 --> 00:10:13,820 you can see, the hit marker turned red and it also played the hit marker sound. 151 00:10:14,000 --> 00:10:19,850 Now let's say I had a shotgun, and let's say half of the pellets hit the face and the other half hit 152 00:10:19,850 --> 00:10:20,240 the body. 153 00:10:20,240 --> 00:10:25,820 So if I shoot like right here, as you can see, I still got a red hit marker, even though half of 154 00:10:25,820 --> 00:10:28,280 my pellets hit the torso of the player. 155 00:10:28,280 --> 00:10:33,110 Now, if I only hit the torso of the player and I shoot again. 156 00:10:33,110 --> 00:10:37,130 As you can see, I'm only getting a white hit marker instead of a red hit marker. 157 00:10:37,130 --> 00:10:41,390 But if I hit the head, as you can see, I'm getting a red hit marker. 158 00:10:43,370 --> 00:10:47,090 Alrighty, that's all I have for you in this lecture and I'll see you in the next one.